#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-07-24 6:01 下午
# @Author  : yangdy
# @File    : 完全平方数
# @Software: PyCharmCE
from typing import List


class Solution:
    def numSquares(self, n: int) -> int:
        from math import sqrt
        ans = 0
        que = [(n, 0)]
        while que:
            que_new = []
            for x in que:
                for i in range(int(sqrt(x[0])), 0, -1):
                    for j in range(1, x//(i*i)):
                        que_new.append((n-i*i*j, j))





